<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\Agent;


use App\Dao\AgentDao;
use App\Dao\LocationDao;
use App\Dao\BusinessDao;
use App\Kernel\Util\AgentJwtInstance;
use App\Service\Service;

use Hyperf\Di\Annotation\Inject;

/**
 * 商家服务
 *
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\Admin
 */
class BusinessService extends Service
{
    /**
     * @Inject
     * @var BusinessDao
     */
    public $business_dao;

    /**
     * @Inject
     * @var LocationDao
     */
    public $location_dao;

    /**
     * @Inject()
     * @var AgentDao
     */
    public $agent_dao;


    /**
     * 商家列表
     */
    public function list()
    {
        $user = AgentJwtInstance::instance()->build()->getAgent();

        $map = [];
        $address = '';
        switch ($user['identity']) {
            case "1": //区
                $map = [
                    'district_id' => $user['location_id'],
                    'status' => 1,
                ];
                $province = $this->location_dao->getLocationByPid( [
                    'pid' => $user['location_id']
                ] );

                $city = $this->location_dao->getLocationByPid( [
                    'pid' => $province['level']
                ] );

                $district = $this->location_dao->getLocationByPid( [
                    'pid' => $city['level']
                ] );
                $address = $district['name'].$city['name'].$province['name'];
                break;
            case "2": //市
                $map = [
                    'city_id' => $user['location_id'],
                    'status' => 1,
                ];

                $city = $this->location_dao->getLocationByPid( [
                    'pid' => $user['location_id']
                ] );

                $district = $this->location_dao->getLocationByPid( [
                    'pid' => $city['level']
                ] );
                $address = $district['name'].$city['name'];
                break;
        }
        $result = $this->business_dao->get($map,false);

        if( $result ) {
            foreach ($result as $key => $value) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date('Y.m.d',$value['created_at']);
                $data['list'][$key]['name'] = $value['name'];
                $data['list'][$key]['legal_name'] = $value['legal_name'];
                $data['list'][$key]['address'] = $value['province'].$value['city'].$value['district'];
                $data['list'][$key]['phone'] = $value['phone'];
                $data['list'][$key]['tel'] = $value['tel'];
                $data['list'][$key]['money_water'] = 0;
                $data['list'][$key]['commission'] = 0;
            }
        }
        $data['agent_address'] = $address;
        $data['agent_type'] = $user['identity'];
        $data['agent_business_count'] = $this->business_dao->count($map);

        return $data;
    }

    /**
     * 获得商家搜索通过条件
     * @param array $params
     * @return array
     */
    public function getBusinessList(array $params)
    {
        $user = AgentJwtInstance::instance()->build()->getAgent();
        $map = [];
        $address = '';
        switch ($user['identity']) {
            case "1": //区
                $map = [
                    'district_id' => $user['location_id'],
                    'status' => 1,
                ];
                $province = $this->location_dao->getLocationByPid( [
                    'pid' => $user['location_id']
                ] );

                $city = $this->location_dao->getLocationByPid( [
                    'pid' => $province['level']
                ] );

                $district = $this->location_dao->getLocationByPid( [
                    'pid' => $city['level']
                ] );
                $address = $district['name'].$city['name'].$province['name'];
                break;
            case "2": //市
                $map = [
                    'city_id' => $user['location_id'],
                    'status' => 1,
                ];

                $city = $this->location_dao->getLocationByPid( [
                    'pid' => $user['location_id']
                ] );

                $district = $this->location_dao->getLocationByPid( [
                    'pid' => $city['level']
                ] );
                $address = $district['name'].$city['name'];
                break;
        }
        $data['agent_business_count'] = $this->business_dao->count($map);
        $data['agent_address'] = $address;
        $data['agent_type'] = $user['identity'];
        // 更多搜索条件
        $map = $this->map($map, $params);

        $sort = 'created_at desc';
        $select = ['id','logo','name','phone','tel','legal_name','created_at','province', 'city', 'district'];
        $page_size = 10;
        $business_list = $this->business_dao->getBusinessByMap($map, $sort, $select, $page_size)->toArray();
        $business_list = array_merge($data, $business_list);


        $business_list['data'] = $this->joinData($business_list['data']);


        return $business_list;


    }

    /**
     * 拼接数据
     * @param array $business_list
     * @return array
     */
    public function joinData(array $business_list)
    {
        foreach ($business_list as &$item) {
            $item['created_at'] = date('Y.m.d',(int)$item['created_at']);
            $item['money_water'] = 0;
            $item['commission'] = 0;
            $item['address'] = $item['province'].$item['city'].$item['district'];
        }

        return $business_list;
    }

    /**
     * 赋值查询条件
     * @param array $map
     * @param array $params
     * @return array
     */
    public function map(array $map, array $params)
    {
        switch ($params['select']??1) {
            case 1: // 时间范围
                isset($params['start_time']) && !isset($params['end_time'])  && !empty($params['start_time']) && $map['created_at'] = ['>=', strtotime($params['start_time'])]; // 下单开始时间
                isset($params['end_time']) &&  !isset($params['start_time'])  && !empty($params['end_time']) && $map['created_at'] = ['<=', strtotime($params['end_time'])]; // 下单结束时间

                isset($params['end_time']) &&  isset($params['start_time']) &&
                !empty($params['end_time']) && !empty($params['start_time']) &&
                $map['between_field'] = ['created_at' =>[strtotime($params['start_time']), strtotime($params['end_time'])]]; // 有下单开始和结束时间
                isset($params['district_id']) && !empty($params['district_id']) && $map['district_id'] = (int)$params['district_id']; // 搜索区商家
                break;
            case 2:
                $params['now_date'] = $params['now_date']??3;
                if ($params['now_date'] == 1) { // 本日
                    // 本日开始
                    $first = date('Y-m-d 00:00:00', time());
                    $first = strtotime($first);
                    // 本日结束
                    $last = date('Y-m-d 23:59:59', time());
                    $last = strtotime($last);
                } elseif ($params['now_date'] == 2) { // 本月
                    // 本月第一天
                    $first = date('Y-m-01 00:00:00', time());
                    // 本月最后一天
                    $last = date('Y-m-d 23:59:59', strtotime("$first +1 month -1 day"));
                    $last = strtotime($last);
                    $first = strtotime($first);

                } else { // 本年
                    // 本年第一天
                    $first = date('Y-01-01 0:0:0', time());
                    $first = strtotime($first);
                    // 本年最后一天
                    $last = date('Y-12-31 23:59:59', time());
                    $last = strtotime($last);
                }
                $map['between_field'] = ['created_at' => [$first, $last]];
                isset($params['district_id']) && !empty($params['district_id']) && $map['district_id'] = (int)$params['district_id']; // 搜索区商家
                break;
            case 3:
                isset($params['search']) && !empty($params['search'])
                && $map['or_field'] = [
                    'name' => ['like', "%{$params['search']}%"],
                    'phone' => ['like', "%{$params['search']}%"],
                    'legal_name' => ['like', "%{$params['search']}%"],
                ];
                break;
            case 4:
                isset($params['district_id']) && !empty($params['district_id']) && $map['district_id'] = (int)$params['district_id']; // 搜索区商家
                break;
            default:
                $this->error("未选择查询");
                break;
        }
        $map['status'] = 1; // 商家状态正常
        return $map;
    }


}